package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.human.domain.InformedConsent;
import com.ruoyi.human.domain.Participant;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.human.service.IParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.ParticipantHealthMonitoring;
import com.ruoyi.human.service.IParticipantHealthMonitoringService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 健康监控Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/monitoring")
public class ParticipantHealthMonitoringController extends BaseController
{
    @Autowired
    private IParticipantHealthMonitoringService participantHealthMonitoringService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    /**
     * 查询健康监控列表
     */
    @RequiresPermissions("human:monitoring:list")
    @GetMapping("/list")
    public TableDataInfo list(ParticipantHealthMonitoring participantHealthMonitoring)
    {
        startPage();
        List<ParticipantHealthMonitoring> list = participantHealthMonitoringService.selectParticipantHealthMonitoringList(participantHealthMonitoring);
        for (ParticipantHealthMonitoring participantHealthMonitoring1:list){
            Long trialId=participantHealthMonitoring1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                participantHealthMonitoring1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        for (ParticipantHealthMonitoring participantHealthMonitoring1:list){
            Long participantId=participantHealthMonitoring1.getParticipantId();
            Participant participant=participantService.selectParticipantByParticipantId(participantId);
            if(participant!=null){
                participantHealthMonitoring1.setParticipantName(participant.getParticipantName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出健康监控列表
     */
    @RequiresPermissions("human:monitoring:export")
    @Log(title = "健康监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ParticipantHealthMonitoring participantHealthMonitoring)
    {
        List<ParticipantHealthMonitoring> list = participantHealthMonitoringService.selectParticipantHealthMonitoringList(participantHealthMonitoring);
        ExcelUtil<ParticipantHealthMonitoring> util = new ExcelUtil<ParticipantHealthMonitoring>(ParticipantHealthMonitoring.class);
        util.exportExcel(response, list, "健康监控数据");
    }

    /**
     * 获取健康监控详细信息
     */
    @RequiresPermissions("human:monitoring:query")
    @GetMapping(value = "/{monitoringId}")
    public AjaxResult getInfo(@PathVariable("monitoringId") Long monitoringId)
    {
        return success(participantHealthMonitoringService.selectParticipantHealthMonitoringByMonitoringId(monitoringId));
    }

    /**
     * 新增健康监控
     */
    @RequiresPermissions("human:monitoring:add")
    @Log(title = "健康监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ParticipantHealthMonitoring participantHealthMonitoring)
    {
        participantHealthMonitoring.setCreatedBy(SecurityUtils.getUsername());
        participantHealthMonitoring.setCreatedAt(new Date());
        return toAjax(participantHealthMonitoringService.insertParticipantHealthMonitoring(participantHealthMonitoring));
    }

    /**
     * 修改健康监控
     */
    @RequiresPermissions("human:monitoring:edit")
    @Log(title = "健康监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ParticipantHealthMonitoring participantHealthMonitoring)
    {
        participantHealthMonitoring.setUpdatedBy(SecurityUtils.getUsername());
        participantHealthMonitoring.setUpdatedAt(new Date());
        participantHealthMonitoring.setVersion(participantHealthMonitoring.getVersion()+1);
        return toAjax(participantHealthMonitoringService.updateParticipantHealthMonitoring(participantHealthMonitoring));
    }

    /**
     * 删除健康监控
     */
    @RequiresPermissions("human:monitoring:remove")
    @Log(title = "健康监控", businessType = BusinessType.DELETE)
	@DeleteMapping("/{monitoringIds}")
    public AjaxResult remove(@PathVariable Long[] monitoringIds)
    {
        for (Long id:monitoringIds){
            ParticipantHealthMonitoring participantHealthMonitoring=participantHealthMonitoringService.selectParticipantHealthMonitoringByMonitoringId(id);
            participantHealthMonitoring.setDeletedAt(new Date());
            participantHealthMonitoring.setDeletedBy(SecurityUtils.getUsername());
            participantHealthMonitoring.setDeletedFlag(1);
            participantHealthMonitoringService.updateParticipantHealthMonitoring(participantHealthMonitoring);
        }
        return toAjax(true);

    }
}
